package com.sntk.organauthomanager.application.mapper;

import com.sntk.organauthomanager.application.dto.AuthResourceVO;
import com.sntk.organauthomanager.application.dto.ResourceDTO;
import com.sntk.organauthomanager.application.dto.ResourceMeta;
import com.sntk.organauthomanager.application.dto.ResourceVO;
import com.sntk.organauthomanager.domain.model.ResourceType;
import com.sntk.organauthomanager.infrastructure.db.entity.ResourceEntity;
import com.sntk.organauthomanager.infrastructure.db.entity.UserEntity;
import com.sntk.organauthomanager.infrastructure.db.repository.ResourceDBRepository;
import com.sntk.organauthomanager.infrastructure.db.repository.UserDBRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @ClassName: ResourceMapper
 * @author: wgchao
 * @createTime: 2020/7/21 4:36 PM
 * @Description: TODO
 */
@Component
public class ResourceMapper {
    @Autowired
    private ResourceDBRepository resourceDBRepository;
    @Autowired
    private UserDBRepository userDBRepository;

    public ResourceDTO transformToDTO(ResourceEntity resourceEntity){
        ResourceDTO resourceDTO = new ResourceDTO();
        BeanUtils.copyProperties(resourceEntity, resourceDTO);
        return resourceDTO;
    }

    public ResourceVO transformToVO(ResourceEntity resourceEntity){
        ResourceVO resourceVO = new ResourceVO();
        BeanUtils.copyProperties(resourceEntity, resourceVO);

        if(!StringUtils.isEmpty(resourceEntity.getParentResource())){
            ResourceEntity parent = resourceDBRepository.findFirstByMarkAndDelFalse(resourceEntity.getParentResource());
            if(parent != null){
                resourceVO.setParentResourceName(parent.getName());
            }
        }
        if(!StringUtils.isEmpty(resourceEntity.getCreator())){
            UserEntity creator = userDBRepository.findFirstByUnameAndDelFalse(resourceEntity.getCreator());
            if(creator != null){
                resourceVO.setCreatorName(creator.getUserName());
            }
        }
        if(!StringUtils.isEmpty(resourceEntity.getLastUpdator())){
            UserEntity updator = userDBRepository.findFirstByUnameAndDelFalse(resourceEntity.getLastUpdator());
            if(updator != null){
                resourceVO.setLastUpdatorName(updator.getUserName());
            }
        }
        //类型名
        if(!StringUtils.isEmpty(resourceVO.getResourceType())){
            resourceVO.setResourceTypeName(ResourceType.get(resourceVO.getResourceType().intValue()).getName());
        }

        return resourceVO;
    }

    public AuthResourceVO transformToAuthVO(ResourceEntity resourceEntity){
        AuthResourceVO resourceVO = new AuthResourceVO();
        resourceVO.setName(resourceEntity.getMark());
        ResourceMeta resourceMeta = new ResourceMeta();
        resourceMeta.setTitle(resourceEntity.getName());
        return resourceVO;
    }

}
